
var activeRecordId;

/**
 * fades in the form for category and resets the recordId
 */
function HandleNewCategory(){
	activeRecordId = null;
	$('#name').val('');
	$('#description').val('');
	$('#form_create_category').fadeIn(1000);
}


/**
 * fades in the form for category and resets the recordId
 */
function HandleNewAccount(){
	activeRecordId = null;
	$('#accountName').val('');
	$('#bankName').val('');
	$('#form_create_account').fadeIn(1000);
}


/**
 * handles saving account
 */
function HandleSaveAccount(){
	if (ValidateAccount()){
		SaveAccount(activeRecordId);
	}
}


/**
 * handles saving category
 */
function HandleSaveCategory(){
	if (ValidateCategory()){
		SaveCategory(activeRecordId);
	}
}



/**
 * saves account
 * @param 		int		accountId
 */
function SaveAccount(accountId){
	var type = (accountId != null) ? 'EDIT' : 'NEW';
	var accountName = encodeURIComponent($('#accountName').val());
	var bankName = encodeURIComponent($('#bankName').val());
	accountId = encodeURIComponent(accountId);
	var ajaxUrl = './cost points/costpoints.php';
	var params = 'T=DML&E=ACCOUNT&TYPE=' + type + '&A=SAVE&ACCOUNTNAME=' + accountName + '&BANKNAME=' + bankName + '&ACCOUNT_ID=' + accountId;

	$.ajax({
		url: ajaxUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(json){
			if (json.RESPONSE == 'SUCCESS'){
				activeRecordId = json.ACCOUNT_ID;
				var params = 'T=AJAX&E=ACCOUNT_TABLE';
				ReloadDiv(ajaxUrl, params, "#account_table");
			}
			else if (json.RESPONSE == 'ERROR')
				alert(json.MESSAGE);
		}
	,
	error: function(resp){
		alert(resp);
	}
	});
}



/**
 * saves category
 * @param		int		categoryId
 */
function SaveCategory(categoryId){
	var type = (categoryId != null) ? 'EDIT' : 'NEW';
	var name = encodeURIComponent($('#name').val());
	var description = encodeURIComponent($('#description').val());
	categoryId = encodeURIComponent(categoryId);
	var ajaxUrl = './cost points/costpoints.php';
	var params = 'T=DML&E=CATEGORY&TYPE=' + type + '&A=SAVE&NAME=' + name + '&DESCRIPTION=' + description + '&CATEGORY_ID=' + categoryId;

	$.ajax({
		url: ajaxUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(json){
			if (json.RESPONSE == 'SUCCESS'){
				activeRecordId = json.CATEGORY_ID;
				var params = 'T=AJAX&E=CATEGORY_TABLE';
				ReloadDiv(ajaxUrl, params, "#category_table");
			}
			else if (json.RESPONSE == 'ERROR')
				alert(json.MESSAGE);
		}
	,
	error: function(resp){
		alert(resp);
	}
	});
}



/**
 * deletes a category and reloads the table
 * @param 		int		categoryId
 */
function DeleteCategory(categoryId){
	var ajaxUrl = './cost points/costpoints.php';
	var params = 'T=DML&E=CATEGORY&A=DELETE&CATEGORY_ID=' + categoryId;

	$.ajax({
		url: ajaxUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(json){
			if (json.RESPONSE == 'SUCCESS'){
				params = 'T=AJAX&E=CATEGORY_TABLE';
				ReloadDiv(ajaxUrl, params, "#category_table");
			}
			else if (json.RESPONSE == 'ERROR')
				alert(json.MESSAGE);
		}
	});
}



/**
 * deletes a category and reloads the table
 * @param 		int		categoryId
 */
function DeleteAccount(accountId){
	var ajaxUrl = './cost points/costpoints.php';
	var params = 'T=DML&E=ACCOUNT&A=DELETE&ACCOUNT_ID=' + accountId;

	$.ajax({
		url: ajaxUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(json){
			if (json.RESPONSE == 'SUCCESS'){
				params = 'T=AJAX&E=ACCOUNT_TABLE';
				ReloadDiv(ajaxUrl, params, "#account_table");
			}
			else if (json.RESPONSE == 'ERROR')
				alert(json.MESSAGE);
		}
	});
}



/**
 * handles the editing of category
 * @param 		int		categoryId
 */
function HandleEditCategory(categoryId){
	activeRecordId = categoryId;
	EditCategory(categoryId);
}


/**
 * handles the editing of account
 * @param		int		 accountId
 */
function HandleEditAccount(accountId){
	activeRecordId = accountId;
	EditAccount(accountId);
}


/**
 * edits the account
 * @param 		int		accountId
 */
function EditAccount(accountId){
		var ajaxUrl = './cost points/costpoints.php';
		var params = 'T=ACCOUNT&E=GET_ACCOUNT&ACCOUNT_ID=' + accountId;

		$.ajax({
			url: ajaxUrl,
			type: 'POST',
			dataType: 'json',
			data: params,
			success: function(json){
				if (json.RESPONSE == 'SUCCESS'){
					$('#bankName').val(json.BANKNAME);
					$('#accountName').val(json.ACCOUNTNAME);
					$('#form_create_account').fadeIn(1000);
				}
				else if (json.RESPONSE == 'ERROR')
					alert(json.MESSAGE);
			}
		});
}


/**
 * edits the category
 * @param 		int		categoryId
 */
function EditCategory(categoryId){
	var ajaxUrl = './cost points/costpoints.php';
	var params = 'T=CATEGORY&E=GET_CATEGORY&CATEGORY_ID=' + categoryId;

	$.ajax({
		url: ajaxUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(json){
			if (json.RESPONSE == 'SUCCESS'){
				$('#name').val(json.NAME);
				$('#description').val(json.DESCRIPTION);
				$('#form_create_category').fadeIn(1000);
			}
			else if (json.RESPONSE == 'ERROR')
				alert(json.MESSAGE);
		}
	});
}
